﻿using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;

public class SceneLoader : Loader
{
	AsyncOperation m_request = null;

	public SceneLoader()
		: base(Loader.LoaderType.SCENE)
	{
		
	}

	public override void Load()
	{
		base.Load();

        if (m_async)
		{
			m_request = SceneManager.LoadSceneAsync(m_path);
		}
		else
		{
			SceneManager.LoadScene(m_path);
			OnLoadCompleted(true);
		}
	}

	protected override void OnLoadProgress(float rate)
	{
		base.OnLoadProgress (rate);
		// 需要走场景进度回调
		LuaHelper.CallLuaFunction(LuaEventDefine.SCENE_PROGRESS, rate);
	}

	public override void Update(float dt)
	{
		if (m_state == LoaderState.LOADING)
		{
			if (m_request == null)
			{
				OnLoadCompleted(false);
			}
			else if (m_request.isDone)
			{
				OnLoadCompleted(true);
				m_request = null;
			}
			else
			{
				OnLoadProgress(m_request.progress);
			}
		}
	}
}